<?php

class MSchedule extends CI_Model{

	public function __construct()
	    {
	        parent::__construct();
	    }
	
   function getFileType(){
		$data = array();
		$this->db->select('file_type_id,type');
	    $Q = $this->db->get('file_type');
	    if ($Q->num_rows() > 0){
	      foreach ($Q->result_array() as $row){
	         $data[$row['file_type_id']] = $row['type'];
	       }
	    }
	    $Q->free_result();  
	    return $data;   
	 }
	 
	function getFileTypeById($id){
		$data = array();
		$this->db->select('type');
		$this->db->where('file_type_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('file_type');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	    $Q->free_result();  
	    return $data;   
	 }
	    
	 function getAllSchedules(){
	     $data = array();
	     $this->db->order_by("academy", "desc");
	     $Q = $this->db->get("schedule");
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	
	 function getSchedule($id){
	 	$data = array();
	    $this->db->where('schedule_id',id_clean($id));
	    $this->db->where('uid',id_clean($_SESSION['uid']));
	    $this->db->limit(1);
	    $Q = $this->db->get('schedule');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
		
	 function addSchedule(){
		$data = array( 
			'description' => db_clean($_POST['description']),
			'semester' => db_clean($_POST['semester']),
			'academy' => db_clean($_POST['academy']),
			'uid' => db_clean($_SESSION['uid'])
		);
		if ($_FILES){
		 	$path = 'images/'.$_SESSION['username'];
		 	if(!is_dir($path))
		    {
		      mkdir($path,0755,TRUE);
		    } 
		    
			$config['upload_path'] = './'.$path;
			$config['allowed_types'] = '*';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '524288';//512 mb
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['schedule_src']['name'])){
				if(!$this->upload->do_upload('schedule_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['schedule_src'] = $image['file_name'];
				}
			}
		}
		$this->db->insert('schedule', $data);	 
	 }
	 
	 function updateSchedule(){
	 	$data = array( 
			'description' => db_clean($_POST['description']),
			'semester' => db_clean($_POST['semester']),
			'academy' => db_clean($_POST['academy']),
			'uid' => db_clean($_SESSION['uid'])
		);
		if ($_FILES){
		 	$path = 'images/'.$_SESSION['username'];
			$config['upload_path'] = './'.$path;
			$config['allowed_types'] = '*';// 'gif|jpg|png|flv|mp4|pdf|ppt';
			$config['max_size'] = '524288';//512 mb
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['schedule_src']['name'])){
				if(!$this->upload->do_upload('schedule_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['schedule_src'] = $image['file_name'];
				}
			}
		}
	 	$this->db->where('schedule_id', id_clean($_POST['schedule_id']));
	 	$this->db->where('uid', id_clean($_SESSION['uid']));
		$this->db->update('schedule', $data);	
	 
	 }
	 
	 
	 function deleteSchedule($id){
	 	$source = $this->db->where('schedule_id', id_clean($id))->get('schedule')->row();
	 	$path = 'images/'.$_SESSION['username'];
	 	if($source->schedule_src != null){
		 	if (file_exists(FCPATH.$path.'/'.$source->schedule_src)) {
		 		unlink($path.'/'.$source->schedule_src);
	    	}else {
	    		$this->session->set_flashdata('error', 'can not delete image from directory.');
	    	}
	 	}	 	
	 	$this->db->where('schedule_id', id_clean($id));
	 	$this->db->where('uid', id_clean($_SESSION['uid']));
		$this->db->delete('schedule');	
	 }
	 
	
}

?>